package com.dyness.poi.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * @author gaohaiqing
 * @since 2024/1/4
 */
public class ExcelLoadUtil {

    public static <T> List<T> loadData(String address, Class<T> data) {
        ImportParams importParams = new ImportParams();
        // 数据处理
        importParams.setHeadRows(1);
        importParams.setTitleRows(0);
        // 需要验证
        importParams.setNeedVerfiy(false);
        importParams.setSheetNum(1);
        List<T> demoList = null;
        try {
            System.out.println("文件地址：" + address);
            File excelFile = new File(address);
            FileInputStream fileInputStream = new FileInputStream(excelFile);
            ExcelImportResult<T> result =
                    ExcelImportUtil.importExcelMore(fileInputStream, data, importParams);
            fileInputStream.close();
            demoList = result.getList();
            System.out.println("----------总条数：" + demoList.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return demoList;
    }
}